$(function () {
    layui.use(['table', 'layer', 'form','laydate'], function () {
        var table = layui.table;
        let layer = layui.layer;
        let form = layui.form;
        let laydate = layui.laydate;
        table.render({
            elem: '#demo'
            , toolbar: '#toolbarDemo'
            , height: 'full-100'
            ,url: '/getAllVaccines' //数据接口
            ,cols: [[ //表头
                {field: 'vNo', title: '编号', width:80,  fixed: 'left'}
                ,{field: 'vName', title: '名称', width:150}
                ,{field: 'vPosition', title: '接种部位', width:150, sort: true}
                ,{field: 'vWay', title: '接种途径', width:150}
                ,{field: 'vDose', title: '接种剂量', width: 100}
                ,{fixed: 'right', title:'操作', toolbar: '#tool', width:270}
            ]]
            ,page: {
                limit:5,
                limits:[1, 5, 10, 40, 100],
                layout:['prev', 'page', 'next','count','limit','refresh']
            } //开启分页
        });
        table.on('tool(vaccines)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            let vNo = data.vNo;
            if(layEvent === 'delete'){ //查看
                layer.open({
                    title: '删除',
                    content: '是否删除此信息'
                    ,btn: ['删除', '取消']
                    ,yes: function(index, layero){
                        //按钮【按钮一】的回调
                        $.post("/delete",{"vNo":vNo},function (da) {
                            if (da.code===200){
                                layer.msg(da.msg)
                                window.location.href = "vaccines.html"
                            }else if (da.code === 500){
                                layer.msg(da.msg)
                                return false
                            }
                        },"json")
                        return false
                    }
                    ,btn2: function(index, layero){
                        //按钮【按钮二】的回调

                        //return false 开启该代码可禁止点击该按钮关闭
                    }
                    ,cancel: function(){
                        //右上角关闭回调

                        //return false 开启该代码可禁止点击该按钮关闭
                    }
                });
                return false;
            }else if (layEvent === 'modify'){
                layer.open({
                    type: 1,
                    title: "疫苗信息修改",
                    area: ['520px', '350px'],
                    content: '<form class="layui-form"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->\n' +
                        '  <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">疫苗名称</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input" id="name" style="width: 400px">\n' +
                        '    </div>\n' +
                        '  </div>' +
                        '  <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">接种部位</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <input type="text" name="position" placeholder="请输入" autocomplete="off" class="layui-input" id="position" style="width: 400px">\n' +
                        '    </div>\n' +
                        '  </div>' +
                        '  <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">接种途径</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <input type="text" name="way" placeholder="请输入" autocomplete="off" class="layui-input" id="way" style="width: 400px">\n' +
                        '    </div>\n' +
                        '  </div>' +
                        '  <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">接种剂量</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <input type="text" name="dose" placeholder="请输入" autocomplete="off" class="layui-input" id="dose" style="width: 400px">\n' +
                        '    </div>\n' +
                        '  </div>' +
                        '<div class="layui-form-item">\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <button class="layui-btn" lay-submit lay-filter="*">修改</button>\n' +
                        '    </div>\n' +
                        '  </div>\n' +
                        '</form>',
                    success: function () {
                        form.render();
                        $("#name").val(data.vName)
                        $("#position").val(data.vPosition)
                        $("#way").val(data.vWay)
                        $("#dose").val(data.vDose)
                        form.on('submit(*)',function (form) {
                            let field = form.field;
                            let s = JSON.stringify(field);
                            layer.open({
                                title: '修改',
                                content: '是否修改'
                                ,btn: ['确定', '取消']
                                ,yes: function(index, layero){
                                        $.post("/update",{"form":s,"vNo":vNo},function (data1) {
                                            if (data1.code===200){
                                                window.location.href = "vaccines.html"
                                            }else if(data1.code === 500){
                                                layer.msg(data1.msg)
                                                return false
                                            }
                                    },"json")
                                }
                                ,btn2: function(index, layero){
                                    //按钮【按钮二】的回调

                                    //return false 开启该代码可禁止点击该按钮关闭
                                }
                                ,cancel: function(){
                                    //右上角关闭回调

                                    //return false 开启该代码可禁止点击该按钮关闭
                                }
                            });
                            return false;
                        })
                    }
                })
                return false;
            }else if (layEvent === 'in'){
                layer.open({
                    type: 1,
                    title: '疫苗进库管理',
                    area: ['550px', '400px'],
                    content: '<form class="layui-form"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->\n' +
                        '  <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">入库时间</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <input type="text" name="iIntime" id="iIntime" placeholder="请输入" autocomplete="off" class="layui-input" style="width: 400px">\n' +
                        '    </div>\n' +
                        '  </div>\n' +
                        '  <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">入库数量</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <input type="text" name="iInnumber" id="iInnumber" placeholder="请输入" autocomplete="off" class="layui-input" style="width: 400px">\n' +
                        '    </div>\n' +
                        '  </div>\n' +
                        '  <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">生产厂家</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <input type="text" name="iManufactor" id="iManufactor" placeholder="请输入" autocomplete="off" class="layui-input" style="width: 400px">\n' +
                        '    </div>\n' +
                        '  </div>\n' +
                        '  <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">管理人</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <input type="text" name="iAdministrator" id="iAdministrator" placeholder="请输入" autocomplete="off" class="layui-input" style="width: 400px">\n' +
                        '    </div>\n' +
                        '  </div>\n' +
                        '  <div class="layui-form-item">\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <button class="layui-btn" lay-submit lay-filter="in">立即提交</button>\n' +
                        '      <button type="reset" class="layui-btn layui-btn-primary">重置</button>\n' +
                        '    </div>\n' +
                        '  </div>\n' +
                        '</form>',
                    success: function(layero, index){
                        form.render();
                        laydate.render({
                            elem: '#iIntime' //指定元素
                        });
                        form.on('submit(in)', function(data){
                            let field = data.field;
                            let s = JSON.stringify(field);
                            $.post("/inRecord",{"form":s,"vNo":vNo},function (data) {
                                if (data.code === 200){
                                   window.location.href = "vaccines.html"
                                }else if (data.code === 500){
                                    layer.msg(data.msg)
                                    return false;
                                }
                                return false;
                            },"json")
                            return false
                        });
                    }
                });
            }else if (layEvent === 'out'){
                layer.open({
                    type: 1,
                    title: '疫苗出库管理',
                    area: ['550px', '350px'],
                    content: '<form class="layui-form"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->\n' +
                        '  <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">出库时间</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <input type="text" name="iOuttime" id="iOuttime" placeholder="请输入" autocomplete="off" class="layui-input" style="width: 400px">\n' +
                        '    </div>\n' +
                        '  </div>\n' +
                        '  <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">出库数量</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <input type="text" name="iOutnumber" id="iOutnumber" placeholder="请输入" autocomplete="off" class="layui-input" style="width: 400px">\n' +
                        '    </div>\n' +
                        '  </div>\n' +
                        '  <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">去向单位</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <input type="text" name="iCompany" id="iCompany" placeholder="请输入" autocomplete="off" class="layui-input" style="width: 400px">\n' +
                        '    </div>\n' +
                        '  </div>\n' +
                        '  <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">管理人</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <input type="text" name="iAdministrator" id="iAdministrator" placeholder="请输入" autocomplete="off" class="layui-input" style="width: 400px">\n' +
                        '    </div>\n' +
                        '  </div>\n' +
                        '  <div class="layui-form-item">\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <button class="layui-btn" lay-submit lay-filter="out">立即提交</button>\n' +
                        '      <button type="reset" class="layui-btn layui-btn-primary">重置</button>\n' +
                        '    </div>\n' +
                        '  </div>\n' +
                        '</form>',
                    success: function(layero, index){
                        form.render();
                        laydate.render({
                            elem: '#iOuttime' //指定元素
                        });
                        form.on('submit(out)', function(data){
                            let field = data.field;
                            let s = JSON.stringify(field);
                            $.post("/outRecord",{"form":s,"vNo":vNo},function (data) {
                                console.log(data)
                                if (data.code === 200){
                                    window.location.href = "vaccines.html"
                                }else if (data.code === 500){
                                    layer.msg(data.msg)
                                    return false;
                                }
                                return false
                            },"json")
                            return false
                        });
                    }
                });
            }
        });
    })
})
function add() {
    layui.use([ 'layer', 'form'], function (){
        let layer = layui.layer;
        let form = layui.form;
        layer.open({
            type: 1,
            title: "添加疫苗信息",
            area: ['520px', '350px'],
            content: '<form class="layui-form"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->\n' +
                '  <div class="layui-form-item">\n' +
                '    <label class="layui-form-label">疫苗名称</label>\n' +
                '    <div class="layui-input-block">\n' +
                '      <input type="text" name="vName" placeholder="请输入" autocomplete="off" class="layui-input" id="vName" style="width: 400px">\n' +
                '    </div>\n' +
                '  </div>' +
                '  <div class="layui-form-item">\n' +
                '    <label class="layui-form-label">接种部位</label>\n' +
                '    <div class="layui-input-block">\n' +
                '      <input type="text" name="vPosition" placeholder="请输入" autocomplete="off" class="layui-input" id="vPosition" style="width: 400px">\n' +
                '    </div>\n' +
                '  </div>' +
                '  <div class="layui-form-item">\n' +
                '    <label class="layui-form-label">接种途径</label>\n' +
                '    <div class="layui-input-block">\n' +
                '      <input type="text" name="vWay" placeholder="请输入" autocomplete="off" class="layui-input" id="vWay" style="width: 400px">\n' +
                '    </div>\n' +
                '  </div>' +
                '  <div class="layui-form-item">\n' +
                '    <label class="layui-form-label">接种剂量</label>\n' +
                '    <div class="layui-input-block">\n' +
                '      <input type="text" name="vDose" placeholder="请输入" autocomplete="off" class="layui-input" id="vDose" style="width: 400px">\n' +
                '    </div>\n' +
                '  </div>' +
                '<div class="layui-form-item">\n' +
                '    <div class="layui-input-block">\n' +
                '      <button class="layui-btn" lay-submit lay-filter="add">修改</button>\n' +
                '    </div>\n' +
                '  </div>\n' +
                '</form>',
            success: function () {
                form.render();
                form.on('submit(add)',function (form) {
                    let field = form.field;
                    let s = JSON.stringify(field);
                    layer.open({
                        title: '添加',
                        content: '是否添加'
                        ,btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            $.post("/add",{"form":s,},function (data2) {
                                if (data2.code === 200){
                                    window.location.href = "vaccines.html"
                                }else if (data2.code === 500){
                                    layer.msg(data2.msg)
                                    return false
                                }
                            },"json")
                            return false
                        }
                        ,btn2: function(index, layero){
                            //按钮【按钮二】的回调

                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                        ,cancel: function(){
                            //右上角关闭回调

                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                    });
                    return false;
                })
            }
        })
    })
}
